#!C:/strawberry/perl/bin/perl.exe

use strict;
use warnings;

use CGI qw(:standard);
use RPC::XML;
use RPC::XML::Client;
use Data::Dumper;
use XML::Simple;
use XML::eXistDB::RPC;
use XML::Compile::RPC::Client;



# read the CGI params
my $cgi = CGI->new;
my $x= $cgi->param("x");
my $y = $cgi->param("y");
my $ciudad = $cgi->param("ciudad");
my $direccion = $cgi->param("direccion");
my $fecha = $cgi->param("fecha");
my $hora = $cgi->param("hora");
#my $dia = $cgi->param("dia");
#my $mes = $cgi->param("mes");
#my $anio = $cgi->param("anio");
#my $cometariosAdicionales = $cgi->param("comentariosAdicionales");
my $tipo = $cgi->param("tipo");
my $descripcion = $cgi->param("descripcion");
my $autor = $cgi->param("autor");
my $id = $cgi->param("id");


my $eXist = XML::eXistDB::RPC->new( destination=>"http://localhost:8080/exist/xmlrpc", repository=>"/db", user=>"admin", password=>"eaf2julian" ) ;



my $xquery = <<END;
    for \$puntero in doc("/placeanddate/markers.xml")/marcadores
    return
	update insert 
            <marcador id="$id">
                <x>$x</x>
                <y>$y</y>
                <fecha>$fecha</fecha>
                <hora>$hora</hora>
                <ciudad>$ciudad</ciudad>
                <direccion>$direccion</direccion>
                <tipo>$tipo</tipo>
                <comentario>$descripcion</comentario>
                <autor>$autor</autor>
            </marcador>
        into doc("/placeanddate/markers.xml")/marcadores
END


my ($rc1, $set) = $eXist->executeQuery($xquery) ;
#my ($rc2, $count) = $eXist->numberOfResults($set) ;
#my ($rc3, @data) = $eXist->retrieveResults($set) ;
#$eXist->releaseResultSet($set) ;


# create a JSON string according to the database result
my $json =qq{{"success" : "marker has been created successfuly"}};
# return JSON string
print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;